import cherrypy
from web_ui import *

'''
Reference: http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions 
'''
def login_screen(from_page='/', username='', error_msg=''):
    template = template_ui()
    '''
     -->> get_parmas = urllib.quote(cherrypy.request.request_line.split()[1])
    if conditions is not None:
        username = cherrypy.session.get(SESSION_KEY)
        if username:
            cherrypy.request.login = username
            for condition in conditions:
                # A condition is just a callable that returns true orfalse
                if not condition():
                    # Send old page as from_page parameter
                    raise cherrypy.HTTPRedirect("/auth/login?from_page=%s" % get_parmas)

    
    '''
    
    
    '''
    Login then send requestor to page rather than home screen...
    '''
    #get_parmas = urllib.quote(cherrypy.request.request_line.split()[1])
    
    
    return template.render('login.html', {'from_page': from_page})


def my_do_login(self, username, password, from_page='..', **kwargs):
        """Login. May raise redirect, or return True if request handled."""
        response = cherrypy.serving.response
        error_msg = self.check_username_and_password(username, password)
        if error_msg:
            body = self.login_screen(from_page, username, error_msg)
            response.body = body
            if "Content-Length" in response.headers:
                # Delete Content-Length header so finalize() recalcs it.
                del response.headers["Content-Length"]
            return True
        else:
            cherrypy.serving.request.login = username
            cherrypy.session[self.session_key] = username
            self.on_login(username)
            raise cherrypy.HTTPRedirect(from_page or "/")
